<?php
/**
 * Created by PhpStorm.
 * User: apple
 * Date: 2020/10/25
 * Time: 10:15 PM
 */
namespace app\index\controller;

use app\BaseController;

use app\model\CleaningPoint;
use app\model\Organization;
use app\model\SignIn;
use app\model\UserSign;
use think\facade\Request;
class Report extends BaseController
{
    /*
     * 考勤日报
     * 领班以上可看
     */
    public function signDaily()
    {
        $param = input('get.');

        //查询时间段
        $startTime = strtotime(date('Y-m-d'));
        $endTime = strtotime(date('Y-m-d 23:59:59'));
        if(isset($param['day']) && !empty($param['day']) && $param['day'] > 0)
        {
            $startTime = strtotime(date('Y-m-d',$param['day']));
            $endTime = strtotime(date('Y-m-d 23:59:59',$param['day']));
        }

        $where = [
            ['sign_time','>=',$startTime],
            ['sign_time','<',$endTime],
            ['sign_status','=',1],
            ['sign_orgid','=',config('user.orgid')],
        ];
//        if(isset($param['type']) && $param['type'] > 0)
//        {
//            $where[] = ['sign_type','=',$param['type']];
//        }

        $signInModel = new SignIn();
        $count = $signInModel->getSignAllCountByWhere($where);
        $list = $signInModel->getSignListByWhere($where);
        $where[] = ['sign_pic','<>',''];
        $info = $signInModel->getHeadSignInfoByWhere($where);
        $rt = [
            'count' => $count,
            'pic' => isset($info) && $info['pic'] ? $info['pic'] : '',
            'signList' => $list
        ];
        success($rt);
    }

    /*
     * 考勤日报
     * 领班以上可看
     */
    public function signDailyV2()
    {
        $param = Request::get();

        $where = [];

        $startTime = strtotime(date('Y-m-d'));
        $endTime = strtotime(date('Y-m-d 23:59:59'));
        if(isset($param['day']) && !empty($param['day']) && $param['day'] > 0)
        {
            $startTime = strtotime(date('Y-m-d',$param['day']));
            $endTime = strtotime(date('Y-m-d 23:59:59',$param['day']));
        }
        $where[] = ['us_start_time','>=',$startTime];
        $where[] = ['us_start_time','<=',$endTime];


        $where[] = ['us_status','=',1];    //默认是 没删除的
        $where[] = ['us_orgid','=',config('user')['orgid']];
        $signModel = new UserSign();

        $list = $signModel->getSignListByWhere($where);
        $count = $signModel->getSignAllCountByWhere($where);
        $where[] = ['us_pic','<>',''];
        $info = $signModel->getHeadSignInfoByWhere($where);
        $rt = [
            'count' => $count,
            'pic' => $info['pic'] ?? '',
            'signList' => $list
        ];
        success($rt);
    }

    /*
     * 巡检月报
     */
    public function inspectionMonth()
    {
        $param = input('get.');

        //查询时间段
        $startTime = strtotime(date('Y-m-01'));
        $endTime = strtotime(date('Y-m-d 23:59:59'));
        if(isset($param['startTime']) && !empty($param['startTime']) && $param['startTime'] > 0)
        {
            $startTime = strtotime(date('Y-m-d',$param['startTime']));
        }

        if(isset($param['endTime']) && !empty($param['endTime']) && $param['endTime'] > 0)
        {
            $endTime = strtotime(date('Y-m-d 23:59:59',$param['endTime']));
        }

        $orgid = config('user.orgid');

        $where = 'ck_create_time>='.$startTime.' && ck_create_time<='.$endTime.' && ck_status=1 && ck_orgid='.$orgid;
        $monthWhere = "datelist>='".date('Y-m-d',$startTime)."' && datelist<='".date('Y-m-d',$endTime)."'";

        if(isset($param['cleaningPointId']) && !empty($param['cleaningPointId']) && $param['cleaningPointId'] > 0)
        {
            $point = intval($param['cleaningPointId']);
            $where .= ' && ck_cleaning_point_id='.$point;
        }

        $cleaningCheckModel = new \app\model\CleaningCheck();
        $count = $cleaningCheckModel->getCheckAllCountByWhere($where);
        $list = $cleaningCheckModel->getCheckMonthListByWhere($where,$monthWhere);
        $rt = [
            'count' => $count,
            'inspectionList' => $list
        ];
        success($rt);
    }

    /*
     * 巡检日报
     */
    public function inspectionDaily()
    {
        $param = input('get.');

        //查询时间段
        $startTime = strtotime(date('Y-m-d'));
        $endTime = strtotime(date('Y-m-d 23:59:59'));
        if(isset($param['day']) && !empty($param['day']) && intval($param['day']) > 0)
        {
            $startTime = strtotime(date('Y-m-d',$param['day']));
            $endTime = strtotime(date('Y-m-d 23:59:59',$param['day']));
        }

        $where = [
            ['ck_create_time','>=',$startTime],
            ['ck_create_time','<',$endTime],
            ['ck_status','=',1],
            ['ck_orgid','=',config('user.orgid')],
        ];


        $cleaningCheckModel = new \app\model\CleaningCheck();
        $count = $cleaningCheckModel->getCheckAllCountByWhere($where);
        $list = $cleaningCheckModel->getCheckListByWhere($where);
        $rt = [
            'count' => $count,
            'inspectionList' => $list
        ];
        success($rt);
    }

    /*
     * 保洁点信息
     */
    public function cleaningPointList()
    {
        $cleaningPointModel = new CleaningPoint();

        $where = [
            'cp_orgid' => config('user.orgid'),
            'cp_status' => 1
        ];
        $list = $cleaningPointModel->getPointListByWhere($where);
        success($list);
    }

    /*
     * 保洁日报
     */
    public function cleaningDaily()
    {
        $param = input('get.');

        //查询时间段
        $startTime = strtotime(date('Y-m-d'));
        $endTime = strtotime(date('Y-m-d 23:59:59'));
        if(isset($param['day']) && !empty($param['day']) && intval($param['day']) > 0)
        {
            $startTime = strtotime(date('Y-m-d',$param['day']));
            $endTime = strtotime(date('Y-m-d 23:59:59',$param['day']));
        }

        $where = [
            ['cleaning_time','>=',$startTime],
            ['cleaning_time','<',$endTime],
            ['cleaning_status','=',1],
            ['cleaning_orgid','=',config('user.orgid')],
        ];


        $cleaningModel = new \app\model\Cleaning();
        $count = $cleaningModel->getCleaningAllCountByWhere($where);
        $list = $cleaningModel->getCleaningListByWhere($where);
        $rt = [
            'count' => $count,
            'cleaningList' => $list
        ];
        success($rt);
    }

    /*
     * 保洁月报
     */
    public function cleaningMonth()
    {
        $param = input('get.');

        //查询时间段
        $startTime = strtotime(date('Y-m-01'));
        $endTime = strtotime(date('Y-m-d 23:59:59'));
        if(isset($param['startTime']) && !empty($param['startTime']) && $param['startTime'] > 0)
        {
            $startTime = strtotime(date('Y-m-d',$param['startTime']));
        }

        if(isset($param['endTime']) && !empty($param['endTime']) && $param['endTime'] > 0)
        {
            $endTime = strtotime(date('Y-m-d 23:59:59',$param['endTime']));
        }

        $orgid = config('user.orgid');

        $where = 'cleaning_time>='.$startTime.' && cleaning_time<='.$endTime.' && cleaning_status=1 && cleaning_orgid='.$orgid;
        $monthWhere = "datelist>='".date('Y-m-d',$startTime)."' && datelist<='".date('Y-m-d',$endTime)."'";

        if(isset($param['cleaningPointId']) && !empty($param['cleaningPointId']) && $param['cleaningPointId'] > 0)
        {
            $point = intval($param['cleaningPointId']);
            $where .= ' && cleaning_point_id='.$point;
        }

        $cleaningModel = new \app\model\Cleaning();
        $count = $cleaningModel->getCleaningAllCountByWhere($where);
        $list = $cleaningModel->getCleaningMonthListByWhere($where,$monthWhere);
        $rt = [
            'count' => $count,
            'cleaningList' => $list
        ];
        success($rt);
    }
    /*
     * 今日清理情况
     */
    public function cleaningToday()
    {
        $param = input('get.');

        if(!isset($param['cleaningPointId']) || empty($param['cleaningPointId']))   fail('请选择保洁点');
        $pointId = intval($param['cleaningPointId']);

        $pointModel = new CleaningPoint();
        $info = $pointModel->getPointInfoById($pointId);
        if(!$info)  fail('保洁点信息不存在');

        $orgModel = new Organization();
        $orgInfo = $orgModel->getOrgInfoById($info['orgid']);
        if(!$orgInfo)   fail('组织信息不存在');
        if($orgInfo['ispersonnel'] == 0)   fail('暂无权限');


        $cleaningModel = new \app\model\Cleaning();

        $where = [
            'cleaning_point_id' => $pointId,
            'cleaning_status' => 1
        ];
        $list = $cleaningModel->getCleaningListByWhere($where);
        success($list);
    }
}